using UnityEngine;
using System.Collections;

public class TranslationScript : MonoBehaviour {
	
	public string m_sTextID;
	private SpriteText m_spriteText;
	private string m_sLastTextID = string.Empty;
	
	public void SetText( string sText )
	{
		m_sTextID = null;
		m_sLastTextID = null;
		ChangeText( sText );
	}
	
	// Use this for initialization
	void Awake()
	{
		m_sLastTextID = null;
		m_spriteText = GetComponent<SpriteText>();
		
		if( m_spriteText == null )
			m_spriteText = GetComponentInChildren<SpriteText>();
		
		if( m_spriteText == null )
		{
			Debug.LogWarning( "## TranslationScript ## spriteText not set in " + name );
			return;
		}
		
//		if ( LangManager.m_bIsInitialized )
		{
			lwLangManager.GetInstance().AddLanguageChangedDelegate( LanguageChange );
		}
	}
	
	void Start()
	{
//		if ( LangManager.m_bIsInitialized )
		{
			lwLangManager.GetInstance().SetCurrentLangFontForSpriteText( m_spriteText );
		}
	}
	
	void OnDestroy()
	{
		lwLangManager.RemoveLanguageDelegate( LanguageChange );
	}
	
	void ChangeText( string sText )
	{
		lwLangManager.GetInstance().SetText( m_spriteText, sText );
	}
	
	void LanguageChange()
	{
//		if ( LangManager.m_bIsInitialized )
		{			
			ChangeText( lwLangManager.GetInstance().GetString( m_sTextID ) );
		}
	}
	
	// Update is called once per frame
	public void Update()
	{
		if ( m_sTextID != m_sLastTextID	)
		{
			m_sLastTextID = m_sTextID;
			LanguageChange();
		}
	}
}
